27. Exercise: Using Navigation Listeners

L3 43 Using Navigation Listeners SC

You can prevent the drawer from being swiped anywhere other than the startDestination. All we need to do is call addOnDestinationChangedListener with a lambda that sets the DrawerLockMode depending on what destination we’re navigating to. When the id of our NavDestination matches the startDestination of our graph, we’ll unlock the drawerLayout; otherwise, we’ll lock and close the drawerLayout.

// prevent nav gesture if not on start destination
navController.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
   if (nd.id == nc.graph.startDestination) {
       drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
   } else {
       drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
   }
}

And that’s it! Having listeners like this makes it powerful to do lots of cool stuff when the user navigates, and keeps the code in a single place in the app.

Task List:

Task Feedback:

Thanks!